// pages/active_gallery/active_gallery.js
const app = getApp();
var router = require("../../router.js");
var tools = require('../../utils/util.js');
var WxParse = require('../../wxParse/wxParse.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    type_id: '',
    event_show_data: '',
    show_input_panel: false,  //是否显示 评论输入框
    fadeIn: '', //过渡动画
    is_mark_like:false, //是否 点赞
    remark_value:'', // 评论输入内容
    pl_lists:'',
    dianzan_lists:'',
    is_dianzan:'',
    click:'',
  },
  // 打开/关闭 评论框
  open_input_panel() {
    if (this.data.show_input_panel) {
      console.log("关闭")
      let animation = wx.createAnimation({
        duration: '300',
        timingFunction: 'ease',
      })
      animation.translate(0, 50).step()
      this.setData({
        fadeIn: animation.export()
      })
      setTimeout(()=>{
        this.setData({
          show_input_panel: false
        })
      },300)
    } else {
      //添加->查询是否评论过
      var that = this
      app.request({
        url: router.index.pinglun_cx,
        method: "POST",
        data: {
          uid: wx.getStorageSync('userInfo')['id'],
          wz_id: that.data.type_id
        },
        success: function (res) {
          if (res>0) {
            wx.showToast({
              title: '你已经评论过',
            }, 1500)
            return false
          }else{
            that.setData({
              show_input_panel: true
            })
            let animation = wx.createAnimation({
              duration: '300',
              timingFunction: 'ease',
            })
            animation.translate(0, 0).step()
            that.setData({
              fadeIn: animation.export()
            })
          }
        }
      })
    }
  },
  // 点赞
  mark_like(){
    var that = this
    if (that.data.is_dianzan > 0){
      wx.showModal({
        title: '提示',
        content: '你已经点过赞，你确定取消点赞吗？',
        success: function(ee){
          if (ee.confirm){
            app.request({
              url: router.index.dianzan_qx,
              method: "POST",
              data: {
                uid: wx.getStorageSync('userInfo')['id'],
                wz_id: that.data.type_id
              },
              success: function (res) {
                if (res) {
                  wx.showToast({
                    title: '点赞取消',
                  }, 1500)
                  that.setData({
                    is_mark_like: false
                  })
                  //刷新评论列表
                  that.dianzan_list();
                }
              }
            })
          }else{
            //用户取消
          }
        }
      })
    }else{
      if (!that.data.click) {
        return false
      }
      that.setData({
        click:false
      })
      app.request({
        url: router.index.dianzan,
        method: "POST",
        data: {
          uid: wx.getStorageSync('userInfo')['id'],
          wz_id: that.data.type_id
        },
        success: function (res) {
          if (res) {
            wx.showToast({
              title: '点赞成功',
            }, 1500)
            that.setData({
              is_mark_like: true,
              click: true
            })
            //刷新评论列表
            that.dianzan_list();
          }
        }
      })
    }
  },
  // 评论内容改变
  remark_input(e){
    let value = e.detail.value;
    this.setData({
      remark_value:value
    })
  },
  // 提交评论
  submit_remark(){
    var that = this
    var content = that.data.remark_value
    if (!content){
      wx.showToast({
        title: '评论内容不能为空',
      })
      return false
    }
    app.request({
      url: router.index.pinglun,
      method: "POST",
      data: { 
        content: content,
        uid: wx.getStorageSync('userInfo')['id'],
        wz_id: that.data.type_id
      },
      success: function (res) {
        if (res) {
          wx.showToast({
            title: '评论成功',
          },1500)
          that.setData({
            show_input_panel: false
          })
          //刷新评论列表
          that.pinglun_list();
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    if (options.type){
      that.setData({
        type:true,
        type_id: options.id,
        click: true
      })
    }else{
      that.setData({
        type: false,
        type_id: options.id,
        click: true
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var type =  that.data.type;
    var url = !type ? router.index.event_show_info : router.index.jieshao_info;
    app.request({
      url: url,
      method: "POST",
      data: { event_show_id: that.data.type_id },
      success: function (res) {
        console.log('sssssssss',res)
        if (res) {
          that.setData({
            event_show_data: res.data,
          });
          WxParse.wxParse('event_show', 'html', res.data.info, that, 5);
        }
      }
    })
    that.dianzan_list();
    that.pinglun_list();
  },
  dianzan_list:function(){
    var that = this
    app.request({
      url: router.index.dianzan_list,
      method: "POST",
      data: {
        uid: wx.getStorageSync('userInfo')['id'],
        wz_id: that.data.type_id
      },
      success: function (res) {
        console.log(res,'resres')
        if (res.status == 1) {
          if (res.data > 0){
            that.setData({
              is_mark_like: true
            })
          }
          that.setData({
            is_dianzan: res.data,
            dianzan_lists: res.dianzan_list,
          })
        }
      }
    })
  },
  pinglun_list:function(){
    var that = this
    app.request({
      url: router.index.pinglun_list,
      method: "POST",
      data: { 
        wz_id: that.data.type_id 
      },
      success: function (res) {
        console.log(res,'res')
        if (res.pinglun_lists) {
          that.setData({
            pl_lists: res.pinglun_lists,
          });
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var wz_id = res.target.dataset.id
    return {
      title: '我的转发',
      path: 'pages/index_new/index_new',
      // imageUrl: '../../img/gg.jpg'
      success:function(){
        app.request({
          url: router.index.zhuangfa,
          method: "POST",
          data: {
            wz_id: wz_id
          },
          success: function (res) {
            console.log(res, 'resres')
            
          }
        })
      }
    }
  }
})